package com.ruicar.afs.cloud.manage.common.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.basic.api.dto.AddressDTO;
import com.ruicar.afs.cloud.basic.api.dto.CustContactVO;
import com.ruicar.afs.cloud.basic.api.dto.RelationDTO;
import com.ruicar.afs.cloud.basic.api.dto.RelationInfoChangeDTO;
import com.ruicar.afs.cloud.basic.api.fegin.loanchange.LoanChangeFegin;
import com.ruicar.afs.cloud.bizcommon.cms.dto.CmsResponse;
import com.ruicar.afs.cloud.bizcommon.cms.enums.CodeResponseEnum;
import com.ruicar.afs.cloud.bizcommon.cms.enums.SuccessResponseEnum;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.*;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.api.dto.gateway.PhoneSyncToCmsDto;
import com.ruicar.afs.cloud.manage.api.feign.gateway.ContractGateWayCall;
import com.ruicar.afs.cloud.manage.api.feign.gateway.ContractGateWayManageCall;
import com.ruicar.afs.cloud.manage.cms.service.IcosToCmsChangeRepaymentService;
import com.ruicar.afs.cloud.manage.common.entity.ManageRelationChange;
import com.ruicar.afs.cloud.manage.common.entity.ManageUpcomeTasksRecord;
import com.ruicar.afs.cloud.manage.common.mapper.ManageRelationChangeMapper;
import com.ruicar.afs.cloud.manage.common.service.ManageRelationChangeService;
import com.ruicar.afs.cloud.manage.common.service.ManageUpcomeTasksRecordService;
import com.ruicar.afs.cloud.manage.common.util.CreatCaseNoUtil;
import com.ruicar.afs.cloud.manage.workflow.entity.WorkTaskAssignInfo;
import com.ruicar.afs.cloud.manage.workflow.enums.NormalSubmitEnum;
import com.ruicar.afs.cloud.manage.workflow.enums.WorkflowTypeEnum;
import com.ruicar.afs.cloud.manage.workflow.processor.ContractWorkFlowSaveApproveInfoProcessor;
import com.ruicar.afs.cloud.manage.workflow.service.WorkflowSubmitService;
import com.ruicar.afs.cloud.manage.workflow.vo.TaskSubmitVO;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysAddressParam;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.WorkFlowResponse;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.request.StartFlowRequest;
import com.ruicar.afs.cloud.workflow.gwt.service.WorkflowService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date 2020-05-30 04:45:15
 * @description
 */
@Service
@AllArgsConstructor
public class ManageRelationChangeServiceImpl extends ServiceImpl<ManageRelationChangeMapper, ManageRelationChange> implements ManageRelationChangeService {

    private final WorkflowService workflowService;
    private final ManageUpcomeTasksRecordService manageUpcomeTasksRecordService;
    private final WorkflowSubmitService workflowSubmitService;
    private final ContractWorkFlowSaveApproveInfoProcessor contractWorkFlowSaveApproveInfoProcessor;
    private final LoanChangeFegin loanChangeFegin;
    private final ContractGateWayCall contractGateWayCall;
    private final ContractGateWayManageCall contractGateWayManageCall;
    private final IcosToCmsChangeRepaymentService icosToCmsChangeRepaymentService;
    @Override
    public WorkFlowResponse contactChangeSubmit(Map map) {
        //先保存再提交
        map.remove("changeInfoJson");
        String s = JSONObject.toJSONString(map);
        ManageRelationChange contactChange = JSON.parseObject(s, ManageRelationChange.class);
        if (EmptyUtils.isEmpty(contactChange.getId()) || contactChange.getId() == null) {
            //无需保存可直接提交
            contactChange.setEndDate(new Date());
            String caseNo = CreatCaseNoUtil.GeneratorCaseNo(ContractBusinessEnum.contactInfoChange);
            contactChange.setCaseNo(caseNo);
            contactChange.setApplyBy(SecurityUtils.getUser().getUserRealName());
            contactChange.setApplyDate(DateUtil.date());
            contactChange.setProcessStatus(ProcessStatusEnum.approved);

            //放入回显字段
            map.put("caseNo", caseNo);
            map.put("id", String.valueOf(contactChange.getId()));
            map.put("applyBy", contactChange.getApplyBy());
            map.put("applyDate", contactChange.getApplyDate());
            s = JSONObject.toJSONString(map);
            map.put("changeInfoJson", s);
            //保存待办任务信息
            contractWorkFlowSaveApproveInfoProcessor.saveApproveInfo(contactChange, ContractBusinessEnum.contactInfoChange,
                    CaseStatusEnum.APPROVED, BusinessUrlEnum.CONTRACT_BUSINESS);
            //首次提交发起申请，保存审批历史记录
            contractWorkFlowSaveApproveInfoProcessor.saveApproveRecord(contactChange, ContractBusinessEnum.contactInfoChange);
            //直接审批通过，修改业务表处理状态
            contactChange.setChangeInfoJson(s);
            save(contactChange);
        } else {
            contactChange.setChangeInfoJson(s);
            //保存待办任务信息
            ManageRelationChange mv = getById(contactChange.getId());
            mv.setEndDate(new Date());
            contactChange.setEndDate(new Date());
            contractWorkFlowSaveApproveInfoProcessor.saveApproveInfo(contactChange, ContractBusinessEnum.contactInfoChange,
                    CaseStatusEnum.APPROVED, BusinessUrlEnum.CONTRACT_BUSINESS);
            //首次提交发起申请，保存审批历史记录
            contractWorkFlowSaveApproveInfoProcessor.saveApproveRecord(contactChange, ContractBusinessEnum.contactInfoChange);
            //直接审批通过，修改业务表处理状态
            mv.setProcessStatus(ProcessStatusEnum.approved);
            mv.setApplyDate(new Date());
            mv.setChangeInfoJson(contactChange.getChangeInfoJson());
            updateById(mv);
        }
        if (DataSourceEnum.icos == contactChange.getDataSource()) {
            //向basic更新最新的联系方式
            Assert.isTrue(CommonConstants.SUCCESS.equals(syncBasicDate(contactChange).getCode()), "提交失败");
        } else {
//            //向cms同步手机号
//            Assert.isTrue(CommonConstants.SUCCESS.equals(syncPhoneCmsData(contactChange).getCode()), "提交失败");
//            //向cms同步地址信息
//            Assert.isTrue(CommonConstants.SUCCESS.equals(syncAddressCmsData(contactChange).getCode()), "提交失败");
            if (contactChange.getChangeType().equals(ContactChangeTypeEnum.PHONE)) {
                //向cms同步手机号
                CmsResponse response = icosToCmsChangeRepaymentService.syncToCmsPhoneResult(contactChange);
                if (AfsEnumUtil.key(SuccessResponseEnum.FAIL).equals(response.getSuccess()) && !AfsEnumUtil.key(CodeResponseEnum.M10051).equals(response.getCode())) {
                    throw new AfsBaseException("手机号变更结果数据推送至cms系统失败，请重试！");
                }
            }else if (contactChange.getChangeType().equals(ContactChangeTypeEnum.ADDRESS)){
                //向cms同步地址信息
                CmsResponse responseResult = icosToCmsChangeRepaymentService.syncToCmsAddressResult(contactChange);
                if (AfsEnumUtil.key(SuccessResponseEnum.FAIL).equals(responseResult.getSuccess()) && !AfsEnumUtil.key(CodeResponseEnum.M10051).equals(responseResult.getCode())) {
                    throw new AfsBaseException("地址变更结果数据推送至cms系统失败，请重试！");
                }
            }else if (contactChange.getChangeType().equals(ContactChangeTypeEnum.PHONE)){
                //向cms同步手机号
                CmsResponse response = icosToCmsChangeRepaymentService.syncToCmsPhoneResult(contactChange);
                if (AfsEnumUtil.key(SuccessResponseEnum.FAIL).equals(response.getSuccess()) && !AfsEnumUtil.key(CodeResponseEnum.M10051).equals(response.getCode())) {
                    throw new AfsBaseException("手机号变更结果数据推送至cms系统失败，请重试！");
                }
                //向cms同步地址信息
                CmsResponse responseResult = icosToCmsChangeRepaymentService.syncToCmsAddressResult(contactChange);
                if (AfsEnumUtil.key(SuccessResponseEnum.FAIL).equals(responseResult.getSuccess()) && !AfsEnumUtil.key(CodeResponseEnum.M10051).equals(responseResult.getCode())) {
                    throw new AfsBaseException("地址变更结果数据推送至cms系统失败，请重试！");
                }
            }


        }
        //发起流程审批
        StartFlowRequest startFlowRequest = new StartFlowRequest();
        startFlowRequest.setAfsFlowKey(AfsEnumUtil.key(WorkflowTypeEnum.CONTACT_CHANGE));
        startFlowRequest.setBizDataId(String.valueOf(contactChange.getId()));
        startFlowRequest.setFlowName((StringUtils.isEmpty(contactChange.getContractNo()) ? "" : (contactChange.getContractNo()) + "-") + AfsEnumUtil.desc(WorkflowTypeEnum.CONTACT_CHANGE));
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(contactChange));
        return workflowService.startWorkFlow(startFlowRequest, json);
    }

    @Override
    public Boolean contactChangeSave(Map map) {
        map.remove("changeInfoJson");
        String s = JSONObject.toJSONString(map);
        ManageRelationChange contactChange = JSON.parseObject(s, ManageRelationChange.class);

        Boolean result;
        if (EmptyUtils.isEmpty(contactChange.getId())) {
            String caseNo = CreatCaseNoUtil.GeneratorCaseNo(ContractBusinessEnum.contactInfoChange);
            //直接将前台数据，转换json存入
            contactChange.setChangeInfoJson(s);
            contactChange.setCaseNo(caseNo);
            contactChange.setApplyBy(SecurityUtils.getUser().getUserRealName());
            contactChange.setProcessStatus(ProcessStatusEnum.draft);
            contactChange.setApplyDate(DateUtil.date());
            result = save(contactChange);

            //放入回显字段
            map.put("caseNo", caseNo);
            map.put("id", String.valueOf(contactChange.getId()));
            map.put("applyBy", contactChange.getApplyBy());
            map.put("applyDate", contactChange.getApplyDate());
            map.put("changeInfoJson", s);
        } else {
            String rs = JSONObject.toJSONString(map);
            contactChange.setChangeInfoJson(rs);
            result = updateById(contactChange);
            map.put("changeInfoJson", s);
        }
        return result;
    }

    @Override
    public void relationChangeAutoApprove() {
        List<ManageUpcomeTasksRecord> tasksRecords = manageUpcomeTasksRecordService.list(Wrappers.<ManageUpcomeTasksRecord>lambdaQuery()
                .eq(ManageUpcomeTasksRecord::getContractBusiness, ContractBusinessEnum.contactInfoChange)
                .eq(ManageUpcomeTasksRecord::getProcessStatus, ProcessStatusEnum.underReview)
                .eq(ManageUpcomeTasksRecord::getProcessStatus, ProcessStatusEnum.underReview));
        for (ManageUpcomeTasksRecord record : tasksRecords) {
            ManageRelationChange manageRelationChange = this.getById(record.getBizDataId());
            TaskSubmitVO vo = new TaskSubmitVO();
            vo.setBizData(JSONObject.parseObject(JSONObject.toJSONString(manageRelationChange)));
            vo.setApprovalOperating(NormalSubmitEnum.SUGGEST_CHECK_FINAL.name());
            WorkTaskAssignInfo workTaskAssignInfo = new WorkTaskAssignInfo();
            vo.setWorkTaskAssignInfo(workTaskAssignInfo);
            workflowSubmitService.submit(vo);


        }
    }


    /**
     * 向basic同步更新最终数据
     */
    private IResponse syncBasicDate(ManageRelationChange contactChange) {
        //调用basic库，修改最新联系方式
        CustContactVO vo = JSON.parseObject(contactChange.getChangeInfoJson(), CustContactVO.class);
        RelationInfoChangeDTO dto = new RelationInfoChangeDTO();
        //手机号变更
        RelationDTO phone = new RelationDTO();
        phone.setCustNo(contactChange.getCustNo());
        phone.setContractNo(contactChange.getContractNo());
        phone.setApplyNo(contactChange.getApplyNo());
        phone.setContactNo(vo.getNewPhone());
        phone.setContactType(ContactTypeEnum.PHONE);
        phone.setCustRole(contactChange.getChangeSubject());
        //单位电话变更
        RelationDTO workPhone = new RelationDTO();
        workPhone.setCustNo(contactChange.getCustNo());
        workPhone.setContractNo(contactChange.getContractNo());
        workPhone.setApplyNo(contactChange.getApplyNo());
        workPhone.setContactNo(vo.getNewWorkPhone());
        workPhone.setContactType(ContactTypeEnum.WORK_PHONE);
        workPhone.setCustRole(contactChange.getChangeSubject());
        //家庭联系方式变更
        RelationDTO homePhone = new RelationDTO();
        homePhone.setCustNo(contactChange.getCustNo());
        homePhone.setContractNo(contactChange.getContractNo());
        homePhone.setApplyNo(contactChange.getApplyNo());
        homePhone.setContactNo(vo.getNewHomeContact());
        homePhone.setContactType(ContactTypeEnum.HOME_CONTACT);
        homePhone.setCustRole(contactChange.getChangeSubject());
        //其他联系方式变更
        RelationDTO otherContact = new RelationDTO();
        otherContact.setCustNo(contactChange.getCustNo());
        otherContact.setContractNo(contactChange.getContractNo());
        otherContact.setApplyNo(contactChange.getApplyNo());
        otherContact.setContactNo(vo.getNewOtherContact());
        otherContact.setContactType(ContactTypeEnum.OTHER_CONTACT);
        otherContact.setCustRole(contactChange.getChangeSubject());
        List<RelationDTO> relationList = new ArrayList<>();
        relationList.add(phone);
        relationList.add(workPhone);
        relationList.add(homePhone);
        relationList.add(otherContact);
        dto.setRelations(relationList);

        //户籍地址变更
        AddressDTO home = new AddressDTO();
        home.setCustNo(contactChange.getCustNo());
        home.setAddressType(AddressTypeEnum.HOME);
        home.setProvince(vo.getNewHomeAddress() != null && vo.getNewHomeAddress().length >= 1 ? vo.getNewHomeAddress()[0] : null);
        home.setCity(vo.getNewHomeAddress() != null && vo.getNewHomeAddress().length >= 2 ? vo.getNewHomeAddress()[1] : null);
        home.setCounty(vo.getNewHomeAddress() != null && vo.getNewHomeAddress().length >= 3 ? vo.getNewHomeAddress()[2] : null);
        home.setStreet(vo.getNewHomeAddress() != null && vo.getNewHomeAddress().length >= 4 ? vo.getNewHomeAddress()[3] : null);
        home.setAddress(vo.getNewHomeAddressDetail());

        //居住地址变更
        AddressDTO live = new AddressDTO();
        live.setCustNo(contactChange.getCustNo());
        live.setAddressType(AddressTypeEnum.LIVE);
        live.setProvince(vo.getNewLiveAddress() != null && vo.getNewLiveAddress().length >= 1 ? vo.getNewLiveAddress()[0] : null);
        live.setCity(vo.getNewLiveAddress() != null && vo.getNewLiveAddress().length >= 2 ? vo.getNewLiveAddress()[1] : null);
        live.setCounty(vo.getNewLiveAddress() != null && vo.getNewLiveAddress().length >= 3 ? vo.getNewLiveAddress()[2] : null);
        live.setStreet(vo.getNewLiveAddress() != null && vo.getNewLiveAddress().length >= 4 ? vo.getNewLiveAddress()[3] : null);
        live.setAddress(vo.getNewLiveAddressDetail());

        //工作地址变更
        AddressDTO work = new AddressDTO();
        work.setCustNo(contactChange.getCustNo());
        work.setAddressType(AddressTypeEnum.WORK);
        work.setProvince(vo.getNewWorkAddress() != null && vo.getNewWorkAddress().length >= 1 ? vo.getNewWorkAddress()[0] : null);
        work.setCity(vo.getNewWorkAddress() != null && vo.getNewWorkAddress().length >= 2 ? vo.getNewWorkAddress()[1] : null);
        work.setCounty(vo.getNewWorkAddress() != null && vo.getNewWorkAddress().length >= 3 ? vo.getNewWorkAddress()[2] : null);
        work.setStreet(vo.getNewWorkAddress() != null && vo.getNewWorkAddress().length >= 4 ? vo.getNewWorkAddress()[3] : null);
        work.setAddress(vo.getNewWorkAddressDetail());

        //邮寄地址变更
        AddressDTO mailing = new AddressDTO();
        mailing.setCustNo(contactChange.getCustNo());
        mailing.setAddressType(AddressTypeEnum.MAILING);
        mailing.setProvince(vo.getNewMailingAddress() != null && vo.getNewMailingAddress().length >= 1 ? vo.getNewMailingAddress()[0] : null);
        mailing.setCity(vo.getNewMailingAddress() != null && vo.getNewMailingAddress().length >= 2 ? vo.getNewMailingAddress()[1] : null);
        mailing.setCounty(vo.getNewMailingAddress() != null && vo.getNewMailingAddress().length >= 3 ? vo.getNewMailingAddress()[2] : null);
        mailing.setStreet(vo.getNewMailingAddress() != null && vo.getNewMailingAddress().length >= 4 ? vo.getNewMailingAddress()[3] : null);
        mailing.setAddress(vo.getNewMailingAddressDetail());

        List<AddressDTO> addressList = new ArrayList<>();
        addressList.add(home);
        addressList.add(live);
        addressList.add(work);
        addressList.add(mailing);
        dto.setAddress(addressList);

        return loanChangeFegin.updateRelation(dto);
    }

    /**
     * 向cms同步更新手机号 只更新手机号
     */
    private IResponse syncPhoneCmsData(ManageRelationChange contactChange) {
        CustContactVO vo = JSON.parseObject(contactChange.getChangeInfoJson(), CustContactVO.class);
        PhoneSyncToCmsDto dto = new PhoneSyncToCmsDto();
        dto.setApplicationNo(contactChange.getContractNo());
        dto.setApplicationPhone(vo.getNewPhone());
        dto.setApprover("admin");
        dto.setOperatorUser(contactChange.getCreateBy());
        dto.setRequestId(contactChange.getId().toString());
        dto.setOrigin("icos");
        return contractGateWayCall.updateRelationToCms(dto);
    }

    /**
     * 向cms同步更新地址
     */
    private IResponse syncAddressCmsData(ManageRelationChange contactChange) {
        CustContactVO vo = JSON.parseObject(contactChange.getChangeInfoJson(), CustContactVO.class);
        PhoneSyncToCmsDto dto = new PhoneSyncToCmsDto();
        //户籍地址
        if (EmptyUtils.isNotEmpty(vo.getNewHomeAddress()) && EmptyUtils.isNotEmpty(vo.getNewHomeAddressDetail())) {
            String province = "";
            String city = "";
            String county = "";
            String street = "";
            if (vo.getNewHomeAddress().length >= 1) {
                //翻译省份
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewHomeAddress()[0]);
                province = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            } else if (vo.getNewHomeAddress().length >= 2) {
                //翻译城市
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewHomeAddress()[1]);
                city = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            } else if (vo.getNewHomeAddress().length >= 3) {
                //翻译区县
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewHomeAddress()[2]);
                county = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            } else if (vo.getNewHomeAddress().length >= 4) {
                //翻译街道
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewHomeAddress()[3]);
                street = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            dto.setApplicationNo(contactChange.getContractNo());
            dto.setAddress(province + city + county + street + vo.getNewHomeAddressDetail());
            dto.setAddressType(AddressTypeEnum.HOME.name());
            dto.setApprover("admin");
            dto.setOperatorUser(contactChange.getCreateBy());
            //RequestId 唯一标记，cms系统防重使用
            dto.setRequestId(contactChange.getId().toString());
            dto.setOrigin("icos");
            return contractGateWayCall.updateRelationToCms(dto);
        }
        //居住地址
        if (EmptyUtils.isNotEmpty(vo.getNewLiveAddress()) && EmptyUtils.isNotEmpty(vo.getNewLiveAddressDetail())) {
            String province = "";
            String city = "";
            String county = "";
            String street = "";
            if (vo.getNewLiveAddress().length >= 1) {
                //翻译省份
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewLiveAddress()[0]);
                province = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewLiveAddress().length >= 2) {
                //翻译城市
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewLiveAddress()[1]);
                city = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewLiveAddress().length >= 3) {
                //翻译区县
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewLiveAddress()[2]);
                county = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewLiveAddress().length >= 4) {
                //翻译街道
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewLiveAddress()[3]);
                street = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            dto.setApplicationNo(contactChange.getContractNo());
            dto.setAddress(province + city + county + street + vo.getNewLiveAddressDetail());
            dto.setAddressType(AddressTypeEnum.LIVE.name());
            dto.setApprover("admin");
            dto.setOperatorUser(contactChange.getCreateBy());
            dto.setRequestId(contactChange.getId().toString());
            dto.setOrigin("icos");
            return contractGateWayCall.updateRelationToCms(dto);
        }
        //工作地址
        if (EmptyUtils.isNotEmpty(vo.getNewWorkAddress()) && EmptyUtils.isNotEmpty(vo.getNewWorkAddressDetail())) {
            String province = "";
            String city = "";
            String county = "";
            String street = "";
            if (vo.getNewWorkAddress().length >= 1) {
                //翻译省份
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewWorkAddress()[0]);
                province = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewWorkAddress().length >= 2) {
                //翻译城市
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewWorkAddress()[1]);
                city = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewWorkAddress().length >= 3) {
                //翻译区县
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewWorkAddress()[2]);
                county = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewWorkAddress().length >= 4) {
                //翻译街道
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewWorkAddress()[3]);
                street = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            dto.setApplicationNo(contactChange.getContractNo());
            dto.setAddress(province + city + county + street + vo.getNewWorkAddressDetail());
            dto.setAddressType(AddressTypeEnum.WORK.name());
            dto.setApprover("admin");
            dto.setOperatorUser(contactChange.getCreateBy());
            dto.setRequestId(contactChange.getId().toString());
            dto.setOrigin("icos");
            return contractGateWayCall.updateRelationToCms(dto);
        }
        //邮寄地址
        if (EmptyUtils.isNotEmpty(vo.getNewMailingAddress()) && EmptyUtils.isNotEmpty(vo.getNewMailingAddressDetail())) {
            String province = "";
            String city = "";
            String county = "";
            String street = "";
            if (vo.getNewMailingAddress().length >= 1) {
                //翻译省份
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewMailingAddress()[0]);
                province = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewMailingAddress().length >= 2) {
                //翻译城市
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewMailingAddress()[1]);
                city = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewMailingAddress().length >= 3) {
                //翻译区县
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewMailingAddress()[2]);
                county = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            if (vo.getNewMailingAddress().length >= 4) {
                //翻译街道
                IResponse<TsysAddressParam> response = contractGateWayManageCall.translateAddress(vo.getNewMailingAddress()[3]);
                street = CommonConstants.SUCCESS.equals(response.getCode()) ? response.getData().getLabel() : "";
            }
            dto.setApplicationNo(contactChange.getContractNo());
            dto.setAddress(province + city + county + street + vo.getNewMailingAddressDetail());
            dto.setAddressType(AddressTypeEnum.MAILING.name());
            dto.setApprover("admin");
            dto.setOperatorUser(contactChange.getCreateBy());
            dto.setRequestId(contactChange.getId().toString());
            dto.setOrigin("icos");
            return contractGateWayCall.updateRelationToCms(dto);
        }
        return new IResponse();

    }

}
